import { createRouter, createWebHistory,RouteRecordRaw  } from 'vue-router';
import HomeView from '../components/homeView/HomeView.vue'
import AboutView from '../components/AboutView.vue'
import LoginView from '../components/LoginView.vue'

const routes : Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        redirect: 'goods',
        component: HomeView,
        children: [{
            path: '/user',
            name: 'user',
            component: () =>  import ('../components/homeView/children/UserView.vue' ),
            meta: {
                title: '用户列表',
                isShow: true,
            }
        },
        {
            path: '/goods',
            name: 'goods',
            component: () => import ('../components/homeView/children/GoodsView.vue' ),
            meta: {
                title: '商品列表',
                isShow: true,

            }
        },{
            path: '/role',
            name: 'role',
            component: () => import ('../components/homeView/children/RoleView.vue'),
            meta: {
                title: '角色列表',
                isShow: true,
            }
        },
        {
            path: '/authority/:id/:authority+',
            name: 'authority',
            component: () => import ('../components/homeView/children/AuthorityView.vue'),
        }
    ]
    },
    {
        path: '/about',
        name: 'about',
        component:AboutView,
    },
    {
        path: '/login',
        name: 'login',
        component: LoginView,
    }
];
const router = createRouter({
    history: createWebHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    const token: string | null = localStorage.getItem('token');
    console.log(to.path)
    if (!token && to.path !== '/login') {
        next('/login');
    } else {
        next();
    }
}) 

export default router;